﻿using System;
using System.IO;
using System.Linq;
using System.Windows;
using AvalonDock;
using CommonPatterns.MVVM;
using Microsoft.Practices.Unity;
using OCDP.Shell.ConfigAccess;
using CommonPatterns.Screen;
using CommonPatterns.IoC;

namespace OCDP.Shell
{
    /// <summary>
    /// Logique d'interaction pour Shell.xaml
    /// </summary>
    public partial class Chrome : IChrome
    {
        private readonly IScreenManagmentConfig _screenManagmentConfig;
        private readonly IUnityContainer _container;

        public Chrome(IUnityContainer container, IScreenManagmentConfig screenManagmentConfig)
        {
            _screenManagmentConfig = screenManagmentConfig;
            _container = container;
            InitializeComponent();

            if(_screenManagmentConfig.UseScreenPersistence)
            {
                dockManager.DeserializationCallback += (s, e) =>
                {
                    e.Content = container.Resolve(e.ViewType) as DockableContent;
                };
                Application.Current.Dispatcher.BeginInvoke(
                    new Action(() =>
                    {
                        if (File.Exists(_screenManagmentConfig.ScreenLayoutFileName))
                            dockManager.RestoreLayout(_screenManagmentConfig.ScreenLayoutFileName);
                    }));
            }
        }

        #region Implementation of IShell

        private void AddMenuItem(Type type, string subMenuItemHeader, System.Windows.Controls.MenuItem menuItem)
        {
            var mi = new System.Windows.Controls.MenuItem { Name = subMenuItemHeader, Header = subMenuItemHeader };
            mi.Click += (a, b) =>
            {
                var instance = _container.Resolve(type) as DockableContent;
                if (instance != null)
                {
                    var attr = Attribute.GetCustomAttributes(instance.GetType()).OfType<MappedToWiewModel>().FirstOrDefault();
                    if (attr != null)
                    {
                        if(!instance.IsActiveContent)
                        {
                            instance.ViewModelType = attr.ViewModelType;
                            instance.Show(dockManager);
                        }
                    }
                }
            };
            if (menuItem != null)
                menuItem.Items.Add(mi);
        }

        public void LoadViewsInMenu<TModule>()
            where TModule : IModule
        {
            var attr = Attribute.GetCustomAttributes(typeof(TModule)).OfType<CommonPatterns.Screen.MenuItem>();
            string header = string.Empty;
            if (attr.Any())
                header = attr.First().MenuItemName;
            else
                header = "_" + typeof(TModule).Name;
            var menuItem = new System.Windows.Controls.MenuItem() { Header = header };
            MenuInstance.Items.Add(menuItem);
            foreach (var instance in _container.FindAllImplementation<IScreenLogic, TModule>())
            {
                var type = instance.GetType();
                var attrScreen = Attribute.GetCustomAttributes(type).OfType<CommonPatterns.Screen.MenuItem>();
                if (attrScreen.Any())
                    AddMenuItem(type, attrScreen.First().MenuItemName, menuItem);
            }
        }

        protected override void OnClosing(System.ComponentModel.CancelEventArgs e)
        {
            if (_screenManagmentConfig.UseScreenPersistence)
                dockManager.SaveLayout(_screenManagmentConfig.ScreenLayoutFileName);
            base.OnClosing(e);
        }

        #endregion
    }
}
